using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers;

/// <summary>
/// 验证码控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class VerificationCodeController : ControllerBase
{
    private readonly IVerificationCodeService _verificationCodeService;

    public VerificationCodeController(IVerificationCodeService verificationCodeService)
    {
        _verificationCodeService = verificationCodeService;
    }

    /// <summary>
    /// 获取所有验证码
    /// </summary>
    [HttpGet]
    public async Task<ActionResult<IEnumerable<VerificationCode>>> GetAll()
    {
        var verificationCodes = await _verificationCodeService.GetAllAsync();
        return Ok(verificationCodes);
    }

    /// <summary>
    /// 根据ID获取验证码
    /// </summary>
    [HttpGet("{id}")]
    public async Task<ActionResult<VerificationCode>> GetById(Guid id)
    {
        var verificationCode = await _verificationCodeService.GetByIdAsync(id);
        if (verificationCode == null)
        {
            return NotFound();
        }
        return Ok(verificationCode);
    }

    /// <summary>
    /// 根据用户ID获取验证码
    /// </summary>
    [HttpGet("user/{userId}")]
    public async Task<ActionResult<VerificationCode>> GetByUserId(Guid userId)
    {
        var verificationCode = await _verificationCodeService.GetByUserIdAsync(userId);
        if (verificationCode == null)
        {
            return NotFound();
        }
        return Ok(verificationCode);
    }

    /// <summary>
    /// 创建验证码
    /// </summary>
    [HttpPost]
    public async Task<ActionResult<VerificationCode>> Create(VerificationCode verificationCode)
    {
        var createdCode = await _verificationCodeService.CreateAsync(verificationCode);
        return CreatedAtAction(nameof(GetById), new { id = createdCode.CodeId }, createdCode);
    }

    /// <summary>
    /// 更新验证码
    /// </summary>
    [HttpPut("{id}")]
    public async Task<IActionResult> Update(Guid id, VerificationCode verificationCode)
    {
        if (id != verificationCode.CodeId)
        {
            return BadRequest();
        }

        await _verificationCodeService.UpdateAsync(verificationCode);
        return NoContent();
    }

    /// <summary>
    /// 删除验证码
    /// </summary>
    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        await _verificationCodeService.DeleteAsync(id);
        return NoContent();
    }

    /// <summary>
    /// 验证验证码
    /// </summary>
    [HttpPost("verify")]
    public async Task<ActionResult<bool>> VerifyCode([FromBody] VerificationRequest request)
    {
        var isValid = await _verificationCodeService.VerifyCodeAsync(
            request.Code, 
            request.CodeType, 
            request.UserId);
        return Ok(isValid);
    }

    /// <summary>
    /// 标记验证码为已使用
    /// </summary>
    [HttpPost("{id}/mark-used")]
    public async Task<IActionResult> MarkAsUsed(Guid id)
    {
        await _verificationCodeService.MarkAsUsedAsync(id);
        return NoContent();
    }
}

/// <summary>
/// 验证码验证请求
/// </summary>
public class VerificationRequest
{
    public string Code { get; set; } = null!;
    public byte CodeType { get; set; }
    public Guid? UserId { get; set; }
} 